#ifndef __LUA_TIMER_H__
#define __LUA_TIMER_H__

#include "common/ObjPool.h"
#include <event2/event.h>
#include <event2/event_struct.h>


struct lua_State;

class LuaTimer
{
    public:
        LuaTimer();
        ~LuaTimer();

        bool Start(struct event_base* pBase, int nInterval, int nTimes);

        static void TimerCallBack(evutil_socket_t nFD, short nEvents, void* pArgs);

        void SetEvent(lua_State *pL, int nObjID, int nEvent);
        void SetParam(int nParam1, int nParam2, int nParam3);
        int GetInputObjID() { return m_nObjID; };

        void CleanUp();

        void ReadyToStop();
        bool Stop();

        inline int  GetPoolID(){
            return m_nPoolID;
        }

        inline void SetPoolID(int nPoolID){
            m_nPoolID = nPoolID;
        }

        inline void SetID(int nTimerID){
            m_nTimerID = nTimerID; 
        }

        inline int GetID(){
            return m_nTimerID;
        }

    private:
        bool IncTimes();

    private:
        int m_nMaxTimes;
        int m_nCurTimes;
        struct event_base *m_pBase;
	    struct event m_oTimerEvent;
        struct timeval m_oTimeVal;
        bool m_bIsEnabled;
        bool m_bIsTimerPersist;
        bool m_bReadyToStop;

        lua_State *m_pL;
        int m_nObjID;
        int m_nEvent;

        int m_nPoolID;
        int m_nTimerID;

        int m_nParam1;
        int m_nParam2;
        int m_nParam3;
};


void RegisterTimer(lua_State* pL);

#endif //__LUA_TIMER_H__

